<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\AssistantUserRel as AssistantUserRelModel;
use app\common\model\Dept as DeptModel;
use app\common\model\StudentInfo as StudentInfoModel;
use app\common\model\User as UserModel;
use app\common\model\Common;
use think\facade\Request;
use think\Db;
use think\facade\Log;

class BookAssistant extends Manage
{

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $post = input('param.');
            
            // 没有实体的model暂时这么处理。
            if(isset($post['limit'])){
                $limit = $post['limit'];
            }else{
                $limit = config('paginate.list_rows');
            }
 
            $page = input('page', 0);
            $index = ($page - 1) * $limit;
    
            $idsString = ''; 
            if(isset($post['pid']) && $post['pid'] != ""){
                $deptModel = new DeptModel();
                $ids = $deptModel->api_getAllDeptIds($post['pid']);
                
                foreach($ids as $val) {
                    $idsString .= ',' . $val;
                }
                
                if (substr($idsString, 0, 1) == ',') {
                    $idsString = substr($idsString, 1);
                }
            } 

            $model = new Common();
            $list = $model->query("call getAssistantInfo('".$idsString."','".$post['mobile']."','".$post['truename']."','".$post['assistant_mobile']."','".$post['assistant_truename']."',".$index.",".$limit.");");
            
            $re['code'] = 0;
            $re['msg'] = '';
            $re['count'] = count($list[1]);
            $re['data'] = $list[0];
    
            return $re;
        }
        return $this->fetch();
    }


    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function assistantUser()
    {
        $this->view->engine->layout(false);
 
        // 这里要排除掉已经是委托的成员
        $userModel = new UserModel();
        $list = $userModel->getBookTeacherList();
        $this->assign('userList', $list);
        
        return $this->fetch('assistantUser');
    }

    /**
     *  助手
     * User:tony
     * @return array|mixed
     */
    public function getAssistantList()
    { 
        $assistantUserRel = new AssistantUserRelModel();
        return $assistantUserRel->tableData(input('param.')); 
    }

    /**
     *  获取所有可能被添加为小助手的人员清单
     * User:tony
     * @return array|mixed
     */
    public function getAssistantInfo()
    {
        $studentInfoModel = new StudentInfoModel();
        $userList = $studentInfoModel->getAssistantInfo(input('param.'));
             
        $result = [
            'status' => true,
            'msg' => '获取成功',
            'data' => $userList
        ];

        return $result; 
    }

    /**
     *  助手 添加
     * User:tony
     * @return array|mixed
     */
    public function assistantUserAdd()
    {
        $assistantUserRel = new AssistantUserRelModel();
        return $assistantUserRel->addData(input('param.'));
    }
 
    /**
     *  助手 软删除
     * User:tony
     * @return array
     */
    public function assistantUserDel()
    {
        $result = ['status'=>true, 'msg'=>'删除成功','data'=>''];

        $assistantUserRel = new AssistantUserRelModel();
        if (!$assistantUserRel->where('assistantUserMobile',input('param.assistantUserMobile/s'))->delete()) {
            $result['status'] = false;
            $result['msg'] = '删除失败';
        }

        return $result;
    }

}